package com.springcloud.sc.ucpv5.service.impl;

import com.springcloud.sc.ucp.v5.api.mongo.locationdb.UserDetailInfo;
import com.springcloud.sc.ucpv5.service.UserDetailInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserDetailInfoServiceImpl implements UserDetailInfoService {

    @Autowired
    @Qualifier(value = "locationmongoTemplate")
    private MongoTemplate locationmongoTemplate;


    @Override
    public UserDetailInfo getUserGPSInfoByNumber(String DN){
        UserDetailInfo setInfo = null;
        try {
            Criteria criteria = new Criteria();
            criteria.andOperator(Criteria.where("DN").in( DN ));
            Query query = new Query(criteria);
            List<UserDetailInfo> userDetailInfoList = locationmongoTemplate.find(query, UserDetailInfo.class);
            if(userDetailInfoList!=null && !userDetailInfoList.isEmpty()){
                setInfo = userDetailInfoList.get(0);
                setInfo.setDN( DN );
            }
        } catch (Exception e) {
            log.error("getUserInfoByFilter Exception");
        }
        return setInfo;
    }

    public UserDetailInfo getUserInfoById(String _id){
        UserDetailInfo setInfo = null;
        try {
            Criteria criteria = new Criteria();
            criteria.andOperator(Criteria.where("_id").in( _id ));
            Query query = new Query(criteria);
            List<UserDetailInfo> userDetailInfoList = locationmongoTemplate.find(query, UserDetailInfo.class);
            if(userDetailInfoList!=null && !userDetailInfoList.isEmpty()){
                setInfo = userDetailInfoList.get(0);
                setInfo.setDN( _id );
            }
        } catch (Exception e) {
            log.error("getUserInfoByFilter Exception");
        }
        return setInfo;
    }


    public List<UserDetailInfo> getUserInfoAll(){
        List<UserDetailInfo> userDetailInfoList = null;
        try {
            userDetailInfoList = locationmongoTemplate.findAll( UserDetailInfo.class );

        } catch (Exception e) {
            log.error("getUserInfoByFilter Exception");
        }

        return userDetailInfoList;
    }


}
